package cn.csc.module.member.controller.app.rolemenu;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.member.controller.app.rolemenu.vo.*;
import cn.csc.module.member.convert.rolemenu.MemberRoleMenuConvert;
import cn.csc.module.member.dal.dataobject.rolemenu.MemberRoleMenuDO;
import cn.csc.module.member.service.rolemenu.MemberRoleMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "用户 APP - 角色和菜单关联")
@RestController
@RequestMapping("/member/role-menu")
@Validated
public class AppMemberRoleMenuController {

    @Resource
    private MemberRoleMenuService roleMenuService;

    @PostMapping("/create")
    @ApiOperation("创建角色和菜单关联")

    public CommonResult<BigInteger> createRoleMenu(@Valid @RequestBody AppMemberRoleMenuCreateReqVO createReqVO) {
        return success(roleMenuService.createRoleMenu(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新角色和菜单关联")

    public CommonResult<Boolean> updateRoleMenu(@Valid @RequestBody AppMemberRoleMenuUpdateReqVO updateReqVO) {
        roleMenuService.updateRoleMenu(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除角色和菜单关联")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)

    public CommonResult<Boolean> deleteRoleMenu(@RequestParam("id") BigInteger id) {
        roleMenuService.deleteRoleMenu(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得角色和菜单关联")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)

    public CommonResult<AppMemberRoleMenuRespVO> getRoleMenu(@RequestParam("id") BigInteger id) {
        MemberRoleMenuDO roleMenu = roleMenuService.getRoleMenu(id);
        return success(MemberRoleMenuConvert.INSTANCE.convert(roleMenu));
    }

    @GetMapping("/list")
    @ApiOperation("获得角色和菜单关联列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)

    public CommonResult<List<AppMemberRoleMenuRespVO>> getRoleMenuList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<MemberRoleMenuDO> list = roleMenuService.getRoleMenuList(ids);
        return success(MemberRoleMenuConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得角色和菜单关联分页")

    public CommonResult<PageResult<AppMemberRoleMenuRespVO>> getRoleMenuPage(@Valid AppMemberRoleMenuPageReqVO pageVO) {
        PageResult<MemberRoleMenuDO> pageResult = roleMenuService.getRoleMenuPage(pageVO);
        return success(MemberRoleMenuConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出角色和菜单关联 Excel")

    @OperateLog(type = EXPORT)
    public void exportRoleMenuExcel(@Valid AppMemberRoleMenuExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MemberRoleMenuDO> list = roleMenuService.getRoleMenuList(exportReqVO);
        // 导出 Excel
        List<AppMemberRoleMenuExcelVO> datas = MemberRoleMenuConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "角色和菜单关联.xls", "数据", AppMemberRoleMenuExcelVO.class, datas);
    }

}
